博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
REST-assured 2发送消息代码重构
阅读量:4450 次
发布时间:2019-06-07

本文共 3834 字,大约阅读时间需要 12 分钟。

将获取token的方法封装到公共类

#javapackage date811;import io.restassured.response.Response;import org.testng.annotations.Test;import static org.junit.Assert.assertNotNull;import static org.junit.Assert.assertThat;import static io.restassured.RestAssured.given;public class WXUtil {    /*    公共类     */    private static String corpID = "xxxxx";    private static String corpSecret = "xxxxxxxx";    private static String tokenURL = "https://qyapi.weixin.qq.com/cgi-bin/gettoken";    //获取access_token的方法    public static String getToken(){        Response res = given().param("corpid",corpID).                param("corpsecret",corpSecret).get(tokenURL).prettyPeek();        String token_text = res.getBody().jsonPath().getString("access_token");        assertNotNull(token_text);        return token_text;    }}

将返回的body封装,便于修改和查看

#javapackage date811;import java.util.Map;public class WXTextMessage {    /**     *     {     *        "touser" : "UserID1|UserID2|UserID3",     *        "toparty" : "PartyID1|PartyID2",     *        "totag" : "TagID1 | TagID2",     *        "msgtype" : "text",     *        "agentid" : 1,     *        "text" : {     *            "content" : "你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看邮件中心视频实况,聪明避开排队。"     *        },     *        "safe":0     *     }     */    private String touser;    private String toparty;    private String totag;    private String msgtype;    private Integer agentid;    private Map
text; private Integer safe; /** * 生成set和get方法 * 右键选择Generate或者control+enter,Getter and Setter全选 */ public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getToparty() { return toparty; } public void setToparty(String toparty) { this.toparty = toparty; } public String getTotag() { return totag; } public void setTotag(String totag) { this.totag = totag; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Integer getAgentid() { return agentid; } public void setAgentid(Integer agentid) { this.agentid = agentid; } public Map
getText() { return text; } public void setText(Map
text) { this.text = text; } public Integer getSafe() { return safe; } public void setSafe(Integer safe) { this.safe = safe; }}

执行类

#javapackage date811;import io.restassured.RestAssured.*;import io.restassured.http.ContentType;import io.restassured.matcher.RestAssuredMatchers.*;import io.restassured.response.Response;import org.hamcrest.Matchers.*;import org.testng.annotations.Test;import java.util.HashMap;import java.util.Map;import static io.restassured.RestAssured.given;import static org.hamcrest.Matchers.equalTo;//assertThat方法一定要静态导入import static org.hamcrest.Matchers.notNullValue;import static org.junit.Assert.assertThat;public class GetToken {    @Test    public void post_message() {        String token_text = WXUtil.getToken();        String postURL = "https://qyapi.weixin.qq.com/cgi-bin/message/send";        WXTextMessage wx = new WXTextMessage();        wx.setAgentid(0);        wx.setSafe(0);        wx.setToparty("0");        wx.setMsgtype("text");        Map
text = new HashMap<>(); text.put("content","你的快递已到,请携带工卡前往邮件中心领取。\\n" + "出发前可查看
" + "邮件中心视频实况,聪明避开排队"); wx.setText(text); Response res = given().contentType(ContentType.JSON).body(wx).queryParam("access_token",token_text) .post(postURL).prettyPeek(); assertThat(res.getBody().jsonPath().getString("errmsg"),equalTo("ok")); }}

1418970-20180811201409512-2031899627.png

转载于:https://www.cnblogs.com/csj2018/p/9460925.html

你可能感兴趣的文章
用python实现矩阵转置
查看>>
linux 小技巧(磁盘空间搜索)
查看>>
iOS开发——捕获崩溃信息
查看>>
(for 循环)编程找出四位整数 abcd 中满足 (ab+cd)(ab+cd)=abcd 的数
查看>>
js 基础
查看>>
tomcat使用spring-loaded实现应用热部署
查看>>
boost1.53中的lock-free
查看>>
链表_leetcode203
查看>>
hdu4746:2013杭州网络赛I 莫比乌斯反演
查看>>
ubuntu linux下火狐跨版本升级方法详解(也同样适合linux下安装火狐中国版)
查看>>
基于ajax 的 几个例子 session ,ajax 实现登录,验证码 ,实现ajax表单展示
查看>>
OSX: 10.9的SMB网络共享连接可能破坏其权限设置
查看>>
连接不上sql server服务器的解决方案
查看>>
记录安装oracle的那些事(二)之双系统安装
查看>>
c3po数据库连接池中取出连接
查看>>
bootstrap-table 分页
查看>>
使用本机IP调试web项目
查看>>
【Java面试题】58 char型变量中能不能存贮一个中文汉字?为什么?
查看>>
C++ Primer 第六章 函数
查看>>
交互设计算法基础(3) - Quick Sort
查看>>